home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 008a / fgdemo10.zip / COMMON.C < prev    next >
Text File  |  1991-10-02  |  14KB  |  534 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  common.c -- routines common to many modules                         *
  4. *                                                                      *
  5. \**********************************************************************/
  6.  
  7. #include "defs.h"
  8.  
  9. int background;
  10.  
  11. int hidden;
  12. int visual;
  13. int vpage;
  14.  
  15. int xlimit;
  16. int ylimit;
  17.  
  18. int old_mode;
  19. int mode;
  20.  
  21. int mode06;
  22. int mode11;
  23. int mode14;
  24. int mode16;
  25.  
  26. int mode06_avail;
  27. int mode11_avail;
  28. int mode14_avail;
  29. int mode16_avail;
  30.  
  31. int mouse;
  32. int xmouse;
  33. int ymouse;
  34. int buttons;
  35.  
  36. char string[80];
  37.  
  38. /**********************************************************************\
  39. *                                                                      *
  40. *  abort_program -- called when the program can't be run               *
  41. *                                                                      *
  42. \**********************************************************************/
  43.  
  44. void abort_program(abort_code)
  45. int abort_code;
  46. {
  47.    /* reset the mode to what it was (probably mode 3) */
  48.  
  49.    fg_setmode(old_mode);
  50.    fg_reset();
  51.  
  52.    /* print a line that will stay on the screen after the program exits */
  53.  
  54.    if (abort_code == 0)
  55.      printf("\nYour system does not have enough memory to run this program.\n");
  56.    else if (abort_code == 1)
  57.      printf("\nThe font file is missing.\n");
  58.    exit(0);
  59. }
  60.  
  61. /**********************************************************************\
  62. *                                                                      *
  63. *  draw_box -- draw a line rectangle in screen space                   *
  64. *                                                                      *
  65. \**********************************************************************/
  66.  
  67. void draw_box(xmin,xmax,ymin,ymax)
  68. int xmin, xmax, ymin, ymax;
  69. {
  70.    fg_move(xmin,ymin);
  71.    fg_draw(xmax,ymin);
  72.    fg_draw(xmax,ymax);
  73.    fg_draw(xmin,ymax);
  74.    fg_draw(xmin,ymin);
  75. }
  76.  
  77. /**********************************************************************\
  78. *                                                                      *
  79. *  draw_tbox -- draw non-graphics single line box                      *
  80. *                                                                      *
  81. \**********************************************************************/
  82.  
  83. void draw_tbox(col1,col2,row1,row2)
  84. int col1, col2, row1, row2;
  85. {
  86.    /* we are working in character space, so use rows and columns */
  87.  
  88.    register int row, col;
  89.  
  90.    /* these are the ASCII characters to make the single line box */
  91.  
  92.    static char *ul = "┌";
  93.    static char *ur = "┐";
  94.    static char *ll = "└";
  95.    static char *lr = "┘";
  96.    static char *horz = "─";
  97.    static char *vert = "│";
  98.  
  99.    /* draw the sides of the box */
  100.  
  101.    for (row = row1+1; row < row2; row++)
  102.    {
  103.       put_tstring(vert,row,col1);
  104.       put_tstring(vert,row,col2);
  105.    }
  106.  
  107.    /* draw the top and the bottom of the box */
  108.  
  109.    for (col = col1+1; col < col2; col++)
  110.    {
  111.       put_tstring(horz,row1,col);
  112.       put_tstring(horz,row2,col);
  113.    }
  114.  
  115.    /* put in the corners */
  116.  
  117.    put_tstring(ul,row1,col1);
  118.    put_tstring(ur,row1,col2);
  119.    put_tstring(ll,row2,col1);
  120.    put_tstring(lr,row2,col2);
  121. }
  122.  
  123. /**********************************************************************\
  124. *                                                                      *
  125. *  exists -- does the file exist?                                      *
  126. *                                                                      *
  127. \**********************************************************************/
  128.  
  129. exists(filename)
  130. char *filename;
  131. {
  132.    if (access(filename,0) == 0)
  133.       return(TRUE);
  134.    else
  135.       return(FALSE);
  136. }
  137.  
  138. /**********************************************************************\
  139. *                                                                      *
  140. *  flushkey -- flush out the keystroke buffer and the mouse buttons    *
  141. *                                                                      *
  142. \**********************************************************************/
  143.  
  144. void flushkey()
  145. {
  146.    int dummy;
  147.    unsigned char key, aux;
  148.  
  149.    if (mouse)
  150.    {
  151.       fg_mousebut(1,&dummy,&dummy,&dummy);
  152.       fg_mousebut(2,&dummy,&dummy,&dummy);
  153.    }
  154.  
  155.    do
  156.       fg_intkey(&key,&aux);
  157.    while (key+aux > 0);
  158. }
  159.  
  160. /**********************************************************************\
  161. *                                                                      *
  162. *          initialize -- initialize the Fastgraph environment          *
  163. *                                                                      *
  164. \**********************************************************************/
  165.  
  166. void initialize()
  167. {
  168.    register int page;
  169.  
  170.    /* Check which video modes are available */
  171.  
  172.    mode06_avail = fg_testmode(6,PAGES);
  173.    mode11_avail = fg_testmode(11,PAGES);
  174.    mode14_avail = fg_testmode(14,PAGES);
  175.    mode16_avail = fg_testmode(16,PAGES);
  176.  
  177.    /* Abort the program if none of the video modes are available */
  178.  
  179.    if (!(mode06_avail | mode11_avail | mode14_avail | mode16_avail))
  180.       abort_program(0);
  181.  
  182.    /* Save the current video mode */
  183.  
  184.    old_mode = fg_getmode();
  185.  
  186.    /* Propose the default video mode */
  187.  
  188.    if (mode16_avail)
  189.       mode = 16;
  190.    else if (mode14_avail)
  191.       mode = 14;
  192.    else if (mode11_avail)
  193.       mode = 11;
  194.    else if (mode06_avail)
  195.       mode = 6;
  196.  
  197.    /* Let the user choose one of the supported modes */
  198.  
  199.    mode = select_mode();
  200.  
  201.    /* Switch to the selected video mode */
  202.  
  203.    fg_setmode(mode);
  204.  
  205.    /* Allocate virtual pages as needed */
  206.  
  207.    for (page = 1; page < PAGES; page++)
  208.       fg_allocate(page);
  209.  
  210.    /* Set up visual and hidden pages */
  211.  
  212.    fg_sethpage(1);
  213.    visual = 0;
  214.    hidden = 1;
  215.    background = 0;
  216.  
  217.    /* Determine maximum screen space coordinates */
  218.  
  219.    xlimit = fg_getmaxx();
  220.    ylimit = fg_getmaxy();
  221.  
  222.    /* For Hercules mode, we'll only use the first 640 of 720 columns */
  223.  
  224.    if (xlimit > 639) xlimit = 639;
  225.  
  226.    /* For 640x350 EGA mode, use only as much vertical resolution as Hercules */
  227.  
  228.    if (ylimit > 347) ylimit = 347;
  229.  
  230.    /* Initialize the mouse, if present */
  231.  
  232.    init_mouse();
  233. }
  234.  
  235. /**********************************************************************\
  236. *                                                                      *
  237. *  init_mouse -- initialize the mouse if present                       *
  238. *                                                                      *
  239. \**********************************************************************/
  240.  
  241. void init_mouse()
  242. {
  243.    if (fg_mouseini() > 0)
  244.    {
  245.       mouse = TRUE;
  246.       fg_mouselim(6,xlimit-5,0,ylimit);
  247.       xmouse = xlimit / 2;
  248.       ymouse = ylimit / 2;
  249.       fg_mousemov(xmouse,ymouse);
  250.       fg_mousevis(ON);
  251.    }
  252.    else
  253.       mouse = FALSE;
  254. }
  255.  
  256. /**********************************************************************\
  257. *                                                                      *
  258. *  printer_ready -- is the printer on line?                            *
  259. *                                                                      *
  260. \**********************************************************************/
  261.  
  262. printer_ready()
  263. {
  264.    static union REGS registers;
  265.  
  266.    registers.h.ah = 2;
  267.    registers.x.dx = 0;
  268.    int86(0x17,®isters,®isters);
  269.    return(registers.h.ah == 0x90);
  270. }
  271.  
  272. /**********************************************************************\
  273. *                                                                      *
  274. *  scale -- the y in the low-res modes is 4/7th the y in hi-res modes  *
  275. *                                                                      *
  276. \**********************************************************************/
  277.  
  278. scale(y)
  279. int y;
  280. {
  281.    if (mode06 || mode14)
  282.       return((y * 4) / 7);
  283.    else
  284.       return(y);
  285. }
  286.  
  287. /**********************************************************************\
  288. *                                                                      *
  289. * select_mode -- let the user choose a supported video mode            *
  290. *                                                                      *
  291. \**********************************************************************/
  292.  
  293. select_mode()
  294. {
  295.    register int i;
  296.    unsigned char key, aux;
  297.    int current, new;
  298.  
  299.    static int max = 4;
  300.  
  301.    static char *menu_string[] = {
  302.       " CGA, MCGA, or Tandy 1000          ",
  303.       " EGA with RGB display              ",
  304.       " EGA with enhanced display, or VGA ",
  305.       " Hercules monochrome               "};
  306.  
  307.    static int menu_row[] = {10,11,12,13};
  308.  
  309.    /* use setmode to clear the screen and initialize Fastgraph */
  310.  
  311.    fg_setmode(old_mode);
  312.  
  313.    /* turn off the cursor and the NumLock key */
  314.  
  315.    fg_cursor(OFF);
  316.    fg_setnum(OFF);
  317.  
  318.    /* draw the box */
  319.  
  320.    fg_setattr(7,7,0);
  321.    fg_rect(21,59,9,14);
  322.    fg_setattr(0,7,0);
  323.    draw_tbox(22,58,9,14);
  324.  
  325.    mode06 = FALSE;
  326.    mode11 = FALSE;
  327.    mode14 = FALSE;
  328.    mode16 = FALSE;
  329.  
  330.    /* start out pointing to preferred mode for this system */
  331.  
  332.    switch(mode)
  333.    {
  334.       case 6:
  335.          current = 0;
  336.          break;
  337.       case 11:
  338.          current = 3;
  339.          break;
  340.       case 14:
  341.          current = 1;
  342.          break;
  343.       case 16:
  344.          current = 2;
  345.    }
  346.  
  347.    new = current;
  348.  
  349.    for (i = 0; i < max; i++)
  350.    {
  351.       fg_setattr(0,7,0);
  352.       put_tstring(menu_string[i],menu_row[i],23);
  353.    }
  354.    fg_setattr(15,0,0);
  355.    put_tstring(menu_string[current],menu_row[current],23);
  356.    put_tstring("Best choice -->",menu_row[current],5);
  357.  
  358.    /* get input and let the user select the mode they want */
  359.  
  360.    while (TRUE)
  361.    {
  362.       fg_getkey(&key,&aux);
  363.  
  364.       if (key == ESC)
  365.           terminate();
  366.  
  367.       else if (aux == DOWN_ARROW)
  368.       {
  369.           new = current + 1;
  370.           if (new >= max) new = 0;
  371.       }
  372.  
  373.       else if (aux == UP_ARROW)
  374.       {
  375.           new = current - 1;
  376.           if (new < 0) new = max - 1;
  377.       }
  378.  
  379.       else if (key == CR)
  380.       {
  381.          switch(current)
  382.          {
  383.             case 0:                /* CGA 2-color mode */
  384.                if (mode06_avail)
  385.                {
  386.                   mode06 = TRUE;
  387.                   return(6);
  388.                }
  389.                else
  390.                {
  391.                   fg_waitfor(1);
  392.                   fg_sound(1200,1);
  393.                   break;
  394.                }
  395.             case 1:               /* 640X200 16 color EGA & VGA */
  396.                if (mode14_avail)
  397.                {
  398.                   mode14 = TRUE;
  399.                   return(14);
  400.                }
  401.                else
  402.                {
  403.                   fg_waitfor(1);
  404.                   fg_sound(1200,1);
  405.                   break;
  406.                }
  407.             case 2:               /* 640X350 16 color EGA & VGA */
  408.                if (mode16_avail)
  409.                {
  410.                   mode16 = TRUE;
  411.                   return(16);
  412.                }
  413.                else
  414.                {
  415.                   fg_waitfor(1);
  416.                   fg_sound(1200,1);
  417.                   break;
  418.                }
  419.             case 3:               /* 720 X 348 Hercules */
  420.                if (mode11_avail)
  421.                {
  422.                   mode11 = TRUE;
  423.                   return(11);
  424.                }
  425.                else
  426.                {
  427.                   fg_waitfor(1);
  428.                   fg_sound(1200,1);
  429.                   break;
  430.                }
  431.          }
  432.       }
  433.  
  434.       /* unhighlight current string */
  435.  
  436.       fg_setattr(0,7,0);
  437.       put_tstring(menu_string[current],menu_row[current],23);
  438.  
  439.       /* highlight the new string */
  440.  
  441.       fg_setattr(15,0,0);
  442.       put_tstring(menu_string[new],menu_row[new],23);
  443.  
  444.       current = new;
  445.    }
  446. }
  447.  
  448. /**********************************************************************\
  449. *                                                                      *
  450. *  terminate -- perform necessary cleanup and return control to DOS    *
  451. *                                                                      *
  452. \**********************************************************************/
  453.  
  454. void terminate()
  455. {
  456.    register int page;
  457.  
  458.    /* Release Fastgraph's virtual video pages */
  459.  
  460.    for (page = 1; page < PAGES; page++)
  461.       fg_freepage(page);
  462.  
  463.    /* Restore the original video mode and screen attributes */
  464.  
  465.    fg_setmode(old_mode);
  466.    fg_reset();
  467.  
  468.    /* Return control to DOS */
  469.  
  470.    exit(0);
  471. }
  472.  
  473. /**********************************************************************\
  474. *                                                                      *
  475. *  wait_for_keystroke -- wait for a keystroke or mouse button          *
  476. *                                                                      *
  477. \**********************************************************************/
  478.  
  479. void wait_for_keystroke()
  480. {
  481.    int buttons;
  482.    int count;
  483.    int x, y;
  484.    unsigned char key, aux;
  485.  
  486.    flushkey();
  487.  
  488.    /* if the mouse is loaded, must loop and wait for button or keystroke */
  489.  
  490.    if (mouse)
  491.    {
  492.       fg_mousevis(ON);
  493.       while (TRUE)
  494.       {
  495.          fg_waitfor(1);
  496.          fg_intkey(&key,&aux);
  497.          if (key+aux > 0) break;
  498.          fg_mousebut(1,&count,&x,&y);
  499.          if (count > 0) break;
  500.          fg_mousebut(2,&count,&x,&y);
  501.          if (count > 0) break;
  502.       }
  503.       do
  504.          fg_mousepos(&x,&y,&buttons);
  505.       while (buttons&3);
  506.       fg_mousevis(OFF);
  507.    }
  508.  
  509.    /* if the mouse is not loaded, just wait for a key */
  510.  
  511.    else
  512.       fg_getkey(&key,&aux);
  513. }
  514.  
  515.  
  516. /**********************************************************************\
  517. *                                                                      *
  518. *  wait_for_mouse_buttons -- wait until no mouse buttons are pressed   *
  519. *                                                                      *
  520. \**********************************************************************/
  521.  
  522. void wait_for_mouse_buttons()
  523. {
  524.    int buttons;
  525.    int x, y;
  526.  
  527.    if (mouse)
  528.    {
  529.       do
  530.          fg_mousepos(&x,&y,&buttons);
  531.       while (buttons&3);
  532.    }
  533. }
  534.